{extend name="public/base" /}

{block name="title"}<title>{if condition="$data"}{$Think.lang.edit}{else /}{$Think.lang.create}{/if}</title>{/block}

{block name="pjax_container"}
<div class="hry-content-wrapper">
<section class="content-header">
    <ol class="breadcrumb">
		<li>企业端管理</li>
		<li class="active">项目回款</li>
		<li class="active">新增项目方</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <form class="form-horizontal" method="POST" action="" onsubmit="return false" >
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_base" data-toggle="tab">{$Think.lang.base_param}</a></li>
                        <li class="pull-right"><a href="javascript:history.back(-1)" class="btn btn-sm" style="padding:10px 2px;"><i class="fa fa-list"></i> {$Think.lang.back}</a></li>
                    </ul>
                    <div class="tab-content">
						<div class="tab-pane active" id="tab_base">
							<div class="form-group">
								<label class="col-sm-2 control-label hry-require">项目方</label>
								<div class="col-sm-7">
                                    <select required class="form-control" name="company_id" id="company">
                                        {foreach name="companyList" item="pro"}
                                        <option value="{$pro.id}">{$pro.company_name}</option>
                                        {/foreach}
                                    </select>
                                </div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label hry-require">项目名称</label>
								<div class="col-sm-7">
                                    <select required class="form-control" name="project_id" id="project">

                                    </select>
                                </div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label hry-require">回款金额</label>
								<div class="col-sm-7"><input required class="form-control type=textarea" name="money" id="money"></div>
							</div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label hry-require">回款备注</label>
                                <div class="col-sm-7"><textarea required class="form-control type=textarea" name="remarks"></textarea></div>
                            </div>
						</div>
                    </div>
                    <div class="box-footer">
                        <div class="col-sm-2"></div>
                        <div class="col-sm-7">
                            <div class="btn-group pull-right">
                                <button type="submit" class="btn btn-info pull-right" data-loading-text="&lt;i class='fa fa-spinner fa-spin '&gt;&lt;/i&gt; {$Think.lang.submit}">{$Think.lang.submit}</button>
                            </div>
                            <div class="btn-group pull-left">
                                <button type="reset" class="btn btn-warning">{$Think.lang.reset}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>
</div>
<script type="text/javascript" src="__STATIC__/hry/js/spark-md5.js"></script>
<script type="text/javascript">

$(function(){
    getProjectList($('#company').children('option:selected').val())
    $('#company').on('change', function () {
        let id = $(this).children('option:selected').val()
        getProjectList(id)
    })
    $("form").submit(function () {
        if(validateForm()){
            var d = {};
            var t = $('form').serializeArray(); // 默认是json 格式
            //jQuery的循环
            $.each(t, function() {
                d[this.name] = this.value;
                console.info(d)
            });
            var json_str = JSON.stringify(t);
            submitForm(d)
        }
    })
    {if condition=" $rest_login == 1 "}
    restlogin('{$rest_login_info}');   //登录超时
    {/if}
})
function submitForm(filed) {
    $.ajax({
        url: '/hryweb.php/project_side/moneyReturnConfirm ',
        data: filed,
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.status == 1) {
                $.amaran({'message': result.info});
                window.location.href = result.url;
            } else {
                $.amaran({'message': result.info});
            }
        },
        error: function (result, xhr, status, errorThrown) {
            $.amaran({'message': result.info})
        },
        complete: function (xhr, status) {
//                    alert('The request is complated');
        },
        beforesend: function () {
        },
        cache: false,//true
        async: true
    })
}
function validateForm() {
    let id = $('#money').val()
    var idreg = /^[0-9]+([.]{1}[0-9]{1,2})?$/;
    let validate = false
    if (!idreg.test(id)) {
        $.amaran({'message': '请输入正确格式的金额数'});
        validate = false
    } else {
        validate = true
    }
    return validate
}
function getProjectList(id) {
    let obj = {
        company_id: id
    }
    $.ajax({
        url: '/hryweb.php/project_side/getProjectList ',
        data: obj,
        type: "POST",
        dataType: "json",
        success: function (result) {
            if (result.status == 1) {
                refreshData(result.list)
            } else {
                $.amaran({'message': result.info});
            }
        },
        error: function (result, xhr, status, errorThrown) {
            $.amaran({'message': result.info})
        },
        complete: function (xhr, status) {
//                    alert('The request is complated');
        },
        beforesend: function () {
        },
        cache: false,//true
        async: true
    })
}
function refreshData(list) {
    let project = $('#project')
    project.empty()
    list.forEach(function (item, index) {
        project.append("<option value='"+item.id+"'>'"+item.project_name+"'</option>")
    })
}
</script>
{/block}